package com.chen.pspms.service.impl;

import com.chen.pspms.entity.Holder;
import com.chen.pspms.mapper.HolderMapper;
import com.chen.pspms.service.HolderSercice;
import com.chen.pspms.utils.ApplicationException;
import com.chen.pspms.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class HolderSerciceImpl implements HolderSercice {
    @Autowired
    HolderMapper holderMapper;

    @Override
    public Integer save(Holder holder) throws ApplicationException {
        int row=0;
        try {
            row = holderMapper.save(holder);
        } catch (Exception e) {
            throw new ApplicationException("新增持证人信息失败", e);
        }
        return row;
    }

    @Override
    public Integer delete(Integer id) throws ApplicationException {
        int row=0;
        try {
            row = holderMapper.delete(id);
        } catch (Exception e) {
            throw new ApplicationException("删除持证人信息失败", e);
        }
        return row;
    }

    @Override
    @RedisCache(key = "#id")
    public Holder findyById(Integer id) throws ApplicationException {
        Holder holder;
        try {
            holder = holderMapper.findyById(id);
        } catch (Exception e) {
            throw new ApplicationException("查询持证人信息失败", e);
        }
        return holder;
    }

    @Override
    @RedisCache(key = "#name")
    public Holder findyByName(String name) throws ApplicationException {
        Holder holder;
        try {
            holder = holderMapper.findyByName(name);
        } catch (Exception e) {
            throw new ApplicationException("通过姓名查询持证人信息失败", e);
        }
        return holder;
    }

    @Override
    public Integer update(Holder holder) throws ApplicationException {
        int row=0;
        try {
            row = holderMapper.update(holder);
        } catch (Exception e) {
            throw new ApplicationException("更新持证人信息失败", e);
        }
        return row;
    }

    @Override
    public List<Holder> findyAll() throws ApplicationException {
        List<Holder> holderlist;
        try {
            holderlist = holderMapper.findyAll();
        } catch (Exception e) {
            throw new ApplicationException("查询持证人信息列表失败", e);
        }
        return holderlist;
    }
}
